package com.tingyun.sign.activity.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tingyun.sign.activity.service.ActivityService;
import com.tingyun.sign.core.entity.ScActivityBean;
import com.tingyun.sign.core.entity.ScUManagerBean;
import com.tingyun.sign.utils.BaseController;
import com.tingyun.sign.utils.DateUtils;
import com.tingyun.sign.utils.Page;

@Controller
@RequestMapping("activity")
public class ActivityController extends BaseController{

	@Autowired
	private ActivityService activityService;

	/**
	 * 添加活动
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public Map<String, Object> add(HttpSession session, ScActivityBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setCuser(manager.getId());
			bean.setMuser(manager.getId());
			bean.setCtime(new Date());	
			bean.setMtime(new Date());
			Integer result = activityService.addActivity(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}
	/**
	 * 编辑活动
	 */
	@RequestMapping(value = "edit")
	@ResponseBody
	public Map<String, Object> edit(HttpSession session, ScActivityBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setMuser(manager.getId());
			Integer result = activityService.updateActivity(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}
	/**
	 * 删除活动
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Map<String, Object> delete(HttpSession session, ScActivityBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setMuser(manager.getId());
			Integer result = activityService.deleteById(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	/**
	 * 活动列表
	 */
	@RequestMapping(value = "list")
	public ModelAndView list(HttpSession session,ScActivityBean bean) {
		bean.setStartTimeStr(DateUtils.converToStringDate(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd"));
		bean.setEndTimeStr(DateUtils.converToStringDate(new Date(),"yyyy-MM-dd"));
		bean.setArea(getSessionUser(session).getType());
		ModelAndView mv = new ModelAndView();
		mv.setViewName("activities/list");
		mv.addObject("obj",bean);
		return mv;
	}
	
	/**
	 * 查找活动
	 */
	@RequestMapping(value = "findPage")
	@ResponseBody
	public Map<String, Object> findPage(HttpSession session, ScActivityBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setArea(manager.getType());
			Page<ScActivityBean> page = activityService.findActivitiesByType(bean);
			map.put("total", page.getTotalCount());
			map.put("rows", page.getResultList());
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}
	
}
